package com.iandun.web.framework.core.configure.environment;

import com.iandun.web.framework.core.configure.context.ApplicationContextHolder;
import com.iandun.web.framework.core.constants.GlobalConstants;
import org.springframework.core.env.Environment;

/**
 * 应用运行环境
 *
 * @author :jason.liang
 * @date: 2021-05-19 19:02
 */
public class ApplicationEnvironment {

    private final static Environment environment
            = ApplicationContextHolder.getBean(Environment.class);

    /**
     * 获取配置文件中的属性
     */
    public static String getProperty(String key) {
        return environment.getProperty(key);
    }

    /**
     * 获取环境属性
     */
    public static EnvEnum getEnv(String env) {
        return EnvEnum.valueToEnum(environment.getProperty(GlobalConstants.ENV_KEY));
    }

}

enum EnvEnum {
    /**
     * 本地开发环境
     */
    DEV("dev"),
    /**
     * 本地测试环境
     */
    LOCAL_TEST("local_test"),
    /**
     * 测试环境
     */
    TEST("test"),
    /**
     * 生产环境
     */
    PRODUCT("product");


    private String env;

    EnvEnum(String env) {
        this.env = env;
    }

    public String getEnv() {
        return env;
    }

    /**
     * 字符串值转换为枚举类
     *
     * @param value 值
     * @return 枚举类
     */
    public static EnvEnum valueToEnum(String value) {
        for (EnvEnum anEnum : values()) {
            if (value.equals(anEnum.getEnv())) {
                return anEnum;
            }
        }
        return null;
    }
}
